﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models.Tables;

namespace WebUI.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        MainContext db = new MainContext();
        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            ViewBag.Href = "/Admin/Product?current=5";
            base.OnActionExecuting(context);
        }
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetList()
        {
            var data = db.Products.Where(m => m.FlagState == true);
            return Json(data);
        }

        [Authorize]
        public ActionResult Create()
        {
            var list = Common.Helper.GetCategoryNoLevel<ProductCategory>(db.ProductCategories.Where(m => m.FlagState == true).OrderBy(m => m.Sort).ToList());
            ViewBag.CatID = new SelectList(list, "ID", "CatName");
            ViewBag.CurrentTitle = "添加";
            return View("Edit", new Product());
        }

        [Authorize]
        public ActionResult Edit(int? id)
        {
            var list = Common.Helper.GetCategoryNoLevel<ProductCategory>(db.ProductCategories.Where(m => m.FlagState == true).OrderBy(m => m.Sort).ToList());
            ViewBag.CatID = new SelectList(list, "ID", "CatName");
            if (id == null)
                throw new Exception("ID错误");
            var model = db.Products.Find(id.Value);
            ViewBag.CurrentTitle = "编辑";
            return View(model);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(Product m)
        {
            try
            {
                m.FlagState = true;
                m.LastModifiedDataTime = DateTime.Now;
                m.LastModifiedUserID = int.Parse(User.Identity.Name);
                if (m.ID == 0)
                {
                    m.CreateDateTime = m.LastModifiedDataTime;
                    db.Products.Add(m);
                }
                else
                {
                    db.Entry(m).State = EntityState.Modified;
                }
                if (db.SaveChanges() == 1)
                    return View("Result", true);
                return View("Result", false);
            }
            catch (Exception)
            {
                return View("Result", false);
            }
        }

        [Authorize]
        public ActionResult Delete(int? id)
        {
            if (id != null)
                try
                {
                    var m = db.Products.Find(id.Value);
                    db.Products.Remove(m);
                    if (db.SaveChanges() == 1)
                        return View("Result", true);
                    return View("Result", false);
                }
                catch (Exception)
                {
                    return View("Result", false);
                }
            return View("Result", false);
        }
    }
}
